
import json
import os
import hou

def get_plugin_config():
    current_dir = os.path.dirname(os.path.abspath(__file__))
    file_path = os.path.join(current_dir, 'plugin_config.json')
    #print(file_path)

    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            config_data = json.load(file)
        #print(config_data)
    except FileNotFoundError:
        print(f"Error: The file '{file_path}' does not exist.")
    except json.JSONDecodeError:
        print(f"Error: The file '{file_path}' contains invalid JSON.")

    finally:
        return(config_data)
    




def load_config(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        config_data = json.load(file)
    

    select_index = config_data[-1]['select']
    # 根据 select 值找到对应的配置项
    selected_config = next((item for item in config_data if item.get('id') == select_index), None)
    
    return selected_config




def use_code_to_node(code):
    nodes = hou.selectedNodes()
    node = nodes[0]
    if(node.type().name()=='attwrangle'):
        node.parm('snippet').set(code)

    elif(node.type().name()=='python'):
        node.parm('python').set(code)

    else:
        parms = node.parms()
        for parm in parms:
            if 'snippet' in parm.name() or 'python' in parm.name():
                parm.set(code)
                break
            else:
                pass



def run_code_to_hou(code):
    pass
